package com.iotsz.user.controller;

import com.iotsz.common.utils.IOTResp;
import com.iotsz.user.bean.User;
import com.iotsz.user.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;


@Controller
@Slf4j
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    @ResponseBody
    @GetMapping("/hello")
    public IOTResp Hello() {
        return IOTResp.success().put("data","hello user");
    }

    @ResponseBody
    @GetMapping("/user")
    public User getUser(@RequestParam("id") Integer id) {
        return userService.getUser(id);
    }


    @ResponseBody
    @PostMapping("/user/add")
    public String addUser(@RequestBody User user) {
        Integer num = userService.addUser(user);
        log.info("num: {}", num);
        return "insert user success";
    }

    @ResponseBody
    @PutMapping("/user/update")
    public String updateUser(@RequestBody User user) {
        userService.updateUser(user);
        return "update user sucsess";
    }

    @ResponseBody
    @DeleteMapping("/user/{id}/delete")
    public String deleteUser(@PathVariable("id") Integer id) {
        userService.deleteUser(id);
        return "delete user success";
    }

}
